package admin

import (
	"context"
	"gozero-demo/internal/constants"
	"gozero-demo/internal/repo/mysql"
	"gozero-demo/internal/svc"
	"gozero-demo/internal/types"
	"gozero-demo/internal/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserDetailLogic struct {
	logx.Logger
	ctx      context.Context
	svcCtx   *svc.ServiceContext
	userRepo *mysql.UserRepo
}

// 用户详细信息
func NewUserDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserDetailLogic {
	return &UserDetailLogic{
		Logger:   logx.WithContext(ctx),
		ctx:      ctx,
		svcCtx:   svcCtx,
		userRepo: mysql.NewUserRepo(ctx, svcCtx),
	}
}

func (l *UserDetailLogic) UserDetail(req *types.UserDetailRequest) (resp *utils.Response, err error) {
	//校验参数,假设这里要求Id必须大于0
	if req.Id <= 0 {
		return nil, constants.CodeParamsError
	}

	/*
		//根据ID查询用户表信息,返回用户详情信息(直接查model层)
		userModel := l.svcCtx.Model.User
		user, err := userModel.WithContext(l.ctx).Debug().Where(userModel.ID.Eq(int64(req.Id))).First()
	*/

	//根据ID查询用户表信息,返回用户详情信息(查repo层)
	user, err := l.userRepo.GetUserInfo(req.Id)
	if err != nil {
		logx.Error("根据ID查询用户表信息失败:" + err.Error())
		return utils.ErrorResponse(constants.CodeServerError.Code, err.Error()), nil
	}

	//成功返回
	return utils.SuccessResponse(types.UserDetailData{
		Id:   user.ID,
		Name: user.Name,
	}), nil
}
